(function () {
    'use strict';
    angular.module('vehicleCheckApp').controller('PendinglistCtrl', PendinglistCtrl);
    function PendinglistCtrl($scope, $state, $stateParams,_DBSerive) {
        var dataLists = [];
		if($stateParams.revise == "true"){
            _DBSerive.queryReviseList(localStorage.actno).then(function(result){
                if(result.rows.length > 0 && result.rows.item(0).changeLists){
                        dataLists = JSON.parse(result.rows.item(0).changeLists);
                        $scope.wrapMethod(dataLists);
                }
            });
			$scope.titleText = "要求整改";
		}else if($stateParams.revise == "false"){
            _DBSerive.queryNotSubmitList(localStorage.actno).then(function(result){
                if(result.rows.length > 0 && result.rows.item(0).notSubmitLists){
                        dataLists = JSON.parse(result.rows.item(0).notSubmitLists);
                        $scope.wrapMethod(dataLists);
                }
            });
			$scope.titleText = "未提交记录";
		}

        $scope.wrapMethod = function(dataLists){
            if(dataLists){
                var nowTime = new Date().getTime();
                angular.forEach(dataLists, function(list){
                    var pastTime = nowTime - list.submitTime;
                    var day2 = 48 - Math.floor(pastTime/(1000*60*60));
                    var hour = 24 - Math.floor(pastTime/(1000*60*60));
                    list.hour = hour;
                    hour > 0 ? list.overdue = false : list.overdue = true;
                    if(day2 <= 0){
                        //超过48小时清除
                        dataLists.splice(dataLists.indexOf(list),1);
                    }
                });
                $scope.dataLists = dataLists;
                console.log("==========24,48小时之后的数据=========");
                console.log($scope.dataLists);
                //重新更新websql的数据
                if($stateParams.revise == "true"){
                    var data = {};
                    data.actno = localStorage.actno;
                    data.changeLists = dataLists;
                    _DBSerive.insertReviseList(data);
                }else if($stateParams.revise == "false"){
                    var data = {};
                    data.actno = localStorage.actno;
                    data.notSubmitLists = dataLists;
                    _DBSerive.insertNotSubmitList(data);
                }
            }
        }

        $scope.dataLength = dataLists.length;
        console.log("dataLength");
        console.log($scope.dataLength);

		
		$scope.goPreview = function(overdue, index){
            console.log("index:"+index);
			if(overdue){
				return false;
			}else{
				$state.go('checklistPreview',{revise:$stateParams.revise, index:index});
			}
		}
		
		$scope.backToHome = function(){
			$state.go("vehicleCheck");
		}
    };
})();
